package com.xmy.cultivate.service.impl;


import com.xmy.cultivate.characteristic.ALiYunSmsCharacteristic;
import com.xmy.cultivate.constants.AliyunCodeConstants;
import com.xmy.cultivate.service.IAliyunApiService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.CommonUtil;
import com.xmy.cultivate.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@Service
public class AliyunApiServiceImpl implements IAliyunApiService {

    @Value("${uploadFile.location}")
    private String uploadFileLocation;//上传文件保存的本地目录，使用@Value获取全局配置文件中配置的属性值，如 E:/wmx/uploadFiles/

    @Value("${aliyun.oss.showImgEndpoint}")
    String showImgEndpoint;

    @Value("${aliyun.tts.appKey}")
    private String appkey;

    @Value("${aliyun.tts.accessKeyId}")
    private String accessKeyId;

    @Value("${aliyun.tts.accessKeySecret}")
    private String accessKeySecret;
    ;

    @Resource
    ALiYunSmsCharacteristic aliyunSmsCharacteristic;

    @Autowired
    RedisTemplate<Object, Object> redisTemplate;





    /**
     * 发送创建试听学员验证码
     * @param phone
     * @param adminId
     * @return
     */
    @Override
    public Result sendVerifyCode(String phone, Long adminId){

        String cacheKey = "verifyCode"+adminId+phone;
        Integer validSeconds = 300;

        Integer oftenTime = validSeconds - 60;//频繁时间，1分钟之内不能发送

        /*Long passExpire = redisTemplate.opsForValue().getOperations().getExpire(cacheKey);
        if (passExpire > oftenTime){
            //return new Result(Code.ERROR,"","请不要频繁操作，稍候再发");
        }*/
        Object code = CommonUtil.generate4DigitNumber();
        aliyunSmsCharacteristic.goToSms(accessKeyId,accessKeySecret,AliyunCodeConstants.SIGN_NAME,AliyunCodeConstants.VERIFY_TEMPLATE_CODE,phone,code.toString());
        redisTemplate.opsForValue().set(cacheKey, code,validSeconds, TimeUnit.SECONDS);
        return new Result(Code.OK,"","");
    }



    /**
     * 发送手机验证码 验证码
     */
    public Result sendCommonVerifyCode(String phone){
        String verifyCommonCode = "verifyCommonCode";
        String cacheKey = verifyCommonCode+phone;
        Integer validSeconds = 300;
        Integer oftenTime = validSeconds - 60;//频繁时间，1分钟之内不能发送
        Long passExpire = redisTemplate.opsForValue().getOperations().getExpire(cacheKey);
        if (passExpire > oftenTime){
            return new Result(Code.ERROR,"","请不要频繁操作，稍候再发");
        }
        Object code = CommonUtil.generate4DigitNumber();
        aliyunSmsCharacteristic.goToSms(accessKeyId,accessKeySecret,AliyunCodeConstants.SIGN_NAME,AliyunCodeConstants.RESUME_VERIFY_TEMPLATE_CODE,phone,code.toString());
        redisTemplate.opsForValue().set(cacheKey, code,validSeconds, TimeUnit.SECONDS);
        return new Result(Code.OK,"","");
    }
    /**
     * 验证手机验证码
     */
    @Override
    public boolean verifyCommonCode(String phone, String verCode){
        String cacheKey = "verifyCommonCode"+phone;
        Object code = redisTemplate.opsForValue().get(cacheKey);
        if(code == null){
            return false;
        }
        if (code.toString().equals(verCode)){
            // 验证成功后删除缓存中的验证码
            redisTemplate.delete(cacheKey);
            return true;
        }else {
            return false;
        }
    }


}
